---
title: "R Notebook"
output: html_notebook
---

```{r}
library(tidyr)
library(here)
library(stargazer)
library(lme4)
library(sjPlot)
library(jtools)
library(sjmisc)
library(lmerTest)
here()
getwd()

n_comparisons = 91
```

```{r}
df = read.csv("./phase_2_within_sample_processing.csv")
```

```{r}
model_score_verylow = lm("score_zscore ~ p1_score_mean_zscore + p1_score_std_zscore + rme_mean_zscore + cogstyle_diversity_q3",
                         data = df %>% dplyr::filter(complexity_cat == "Very low"))
model_score_low = lm("score_zscore ~ p1_score_mean_zscore + p1_score_std_zscore + rme_mean_zscore + cogstyle_diversity_q3",
                         data = df %>% dplyr::filter(complexity_cat == "Low"))
model_score_moderate = lm("score_zscore ~ p1_score_mean_zscore + p1_score_std_zscore + rme_mean_zscore + cogstyle_diversity_q3",
                         data = df %>% dplyr::filter(complexity_cat == "Moderate"))
model_score_high = lm("score_zscore ~ p1_score_mean_zscore + p1_score_std_zscore + rme_mean_zscore + cogstyle_diversity_q3",
                         data = df %>% dplyr::filter(complexity_cat == "High"))
model_score_veryhigh = lm("score_zscore ~ p1_score_mean_zscore + p1_score_std_zscore + rme_mean_zscore + cogstyle_diversity_q3",
                         data = df %>% dplyr::filter(complexity_cat == "Very high"))
model_score_overall = lmer("score_zscore ~ p1_score_mean_zscore + p1_score_std_zscore + rme_mean_zscore + cogstyle_diversity_q3 + (1|game_id)",data=df)

model_duration_verylow = lm("round_duration_zscore ~ p1_score_mean_zscore + p1_score_std_zscore + rme_mean_zscore + cogstyle_diversity_q3",
                         data = df %>% dplyr::filter(complexity_cat == "Very low"))
model_duration_low = lm("round_duration_zscore ~ p1_score_mean_zscore + p1_score_std_zscore + rme_mean_zscore + cogstyle_diversity_q3",
                         data = df %>% dplyr::filter(complexity_cat == "Low"))
model_duration_moderate = lm("round_duration_zscore ~ p1_score_mean_zscore + p1_score_std_zscore + rme_mean_zscore + cogstyle_diversity_q3",
                         data = df %>% dplyr::filter(complexity_cat == "Moderate"))
model_duration_high = lm("round_duration_zscore ~ p1_score_mean_zscore + p1_score_std_zscore + rme_mean_zscore + cogstyle_diversity_q3",
                         data = df %>% dplyr::filter(complexity_cat == "High"))
model_duration_veryhigh = lm("round_duration_zscore ~ p1_score_mean_zscore + p1_score_std_zscore + rme_mean_zscore + cogstyle_diversity_q3",
                         data = df %>% dplyr::filter(complexity_cat == "Very high"))
model_duration_overall = lmer("round_duration_zscore ~ p1_score_mean_zscore + p1_score_std_zscore + rme_mean_zscore + cogstyle_diversity_q3 + (1|game_id)",
                           data=df)

model_efficiency_verylow = lm("efficiency_zscore ~ p1_score_mean_zscore + p1_score_std_zscore + rme_mean_zscore + cogstyle_diversity_q3",
                         data = df %>% dplyr::filter(complexity_cat == "Very low"))
model_efficiency_low = lm("efficiency_zscore ~ p1_score_mean_zscore + p1_score_std_zscore + rme_mean_zscore + cogstyle_diversity_q3",
                         data = df %>% dplyr::filter(complexity_cat == "Low"))
model_efficiency_moderate = lm("efficiency_zscore ~ p1_score_mean_zscore + p1_score_std_zscore + rme_mean_zscore + cogstyle_diversity_q3",
                         data = df %>% dplyr::filter(complexity_cat == "Moderate"))
model_efficiency_high = lm("efficiency_zscore ~ p1_score_mean_zscore + p1_score_std_zscore + rme_mean_zscore + cogstyle_diversity_q3",
                         data = df %>% dplyr::filter(complexity_cat == "High"))
model_efficiency_veryhigh = lm("efficiency_zscore ~ p1_score_mean_zscore + p1_score_std_zscore + rme_mean_zscore + cogstyle_diversity_q3",
                         data = df %>% dplyr::filter(complexity_cat == "Very high"))
model_efficiency_overall = lmer("efficiency_zscore ~ p1_score_mean_zscore + p1_score_std_zscore + rme_mean_zscore + cogstyle_diversity_q3 + (1|game_id)",
                           data=df)

varnames = c("p1_score_mean_zscore", "p1_score_std_zscore", "rme_mean_zscore", "cogstyle_diversity_q3True")
names(varnames) = c("Skill","Skill diversity","Social perceptiveness", "Cognitive style diversity")

ht_score = export_summs(model_score_verylow,
          model_score_low,
          model_score_moderate,
          model_score_high,
          model_score_veryhigh,
          model_score_overall,
          model.names = c("Very Low","Low","Moderate","High","Very High","Overall"),
          coefs=varnames,
          error_format = "({std.error})\nP = {p.value}",
          error_pos = "same",
          digits=3,
          statistics=c(N="nobs"))

ht_duration = export_summs(model_duration_verylow,
          model_duration_low,
          model_duration_moderate,
          model_duration_high,
          model_duration_veryhigh,
          model_duration_overall,
          model.names = c("Very Low","Low","Moderate","High","Very High","Overall"),
          coefs=varnames,
          error_format = "({std.error})\nP = {p.value}",
          error_pos = "same",
          digits=3,
          statistics=c(N="nobs"))

ht_efficiency = export_summs(model_efficiency_verylow,
          model_efficiency_low,
          model_efficiency_moderate,
          model_efficiency_high,
          model_efficiency_veryhigh,
          model_efficiency_overall,
          model.names = c("Very Low","Low","Moderate","High","Very High","Overall"),
          coefs=varnames,
          error_format = "({std.error})\nP = {p.value}",
          error_pos = "same",
          digits=3,
          statistics=c(N="nobs"))


wb = openxlsx::createWorkbook()
wb = huxtable::as_Workbook(ht_score, Workbook=wb, sheet = "Comp v. Score")
wb = huxtable::as_Workbook(ht_duration, Workbook=wb, sheet = "Comp v. Duration")
wb = huxtable::as_Workbook(ht_efficiency, Workbook=wb, sheet = "Comp v. Efficiency")
# openxlsx::saveWorkbook(wb, "test.xlsx", overwrite=T)



model_collab_score_verylow = lm("score_zscore ~ n_chats_round_zscore + turn_taking_index_zscore",
                         data = df %>% dplyr::filter(complexity_cat == "Very low"))
model_collab_score_low = lm("score_zscore ~ n_chats_round_zscore + turn_taking_index_zscore",
                         data = df %>% dplyr::filter(complexity_cat == "Low"))
model_collab_score_moderate = lm("score_zscore ~ n_chats_round_zscore + turn_taking_index_zscore",
                         data = df %>% dplyr::filter(complexity_cat == "Moderate"))
model_collab_score_high = lm("score_zscore ~ n_chats_round_zscore + turn_taking_index_zscore",
                         data = df %>% dplyr::filter(complexity_cat == "High"))
model_collab_score_veryhigh = lm("score_zscore ~ n_chats_round_zscore + turn_taking_index_zscore",
                         data = df %>% dplyr::filter(complexity_cat == "Very high"))
model_collab_score_overall = lmer("score_zscore ~ n_chats_round_zscore + turn_taking_index_zscore + (1|game_id)",
                           data=df)

model_collab_duration_verylow = lm("round_duration_zscore ~ n_chats_round_zscore + turn_taking_index_zscore",
                         data = df %>% dplyr::filter(complexity_cat == "Very low"))
model_collab_duration_low = lm("round_duration_zscore ~ n_chats_round_zscore + turn_taking_index_zscore",
                         data = df %>% dplyr::filter(complexity_cat == "Low"))
model_collab_duration_moderate = lm("round_duration_zscore ~ n_chats_round_zscore + turn_taking_index_zscore",
                         data = df %>% dplyr::filter(complexity_cat == "Moderate"))
model_collab_duration_high = lm("round_duration_zscore ~ n_chats_round_zscore + turn_taking_index_zscore",
                         data = df %>% dplyr::filter(complexity_cat == "High"))
model_collab_duration_veryhigh = lm("round_duration_zscore ~ n_chats_round_zscore + turn_taking_index_zscore",
                         data = df %>% dplyr::filter(complexity_cat == "Very high"))
model_collab_duration_overall = lmer("round_duration_zscore ~ n_chats_round_zscore + turn_taking_index_zscore + (1|game_id)",
                           data=df)

model_collab_efficiency_verylow = lm("efficiency_zscore ~ n_chats_round_zscore + turn_taking_index_zscore",
                         data = df %>% dplyr::filter(complexity_cat == "Very low"))
model_collab_efficiency_low = lm("efficiency_zscore ~ n_chats_round_zscore + turn_taking_index_zscore",
                         data = df %>% dplyr::filter(complexity_cat == "Low"))
model_collab_efficiency_moderate = lm("efficiency_zscore ~ n_chats_round_zscore + turn_taking_index_zscore",
                         data = df %>% dplyr::filter(complexity_cat == "Moderate"))
model_collab_efficiency_high = lm("efficiency_zscore ~ n_chats_round_zscore + turn_taking_index_zscore",
                         data = df %>% dplyr::filter(complexity_cat == "High"))
model_collab_efficiency_veryhigh = lm("efficiency_zscore ~ n_chats_round_zscore + turn_taking_index_zscore",
                         data = df %>% dplyr::filter(complexity_cat == "Very high"))
model_collab_efficiency_overall = lmer("efficiency_zscore ~ n_chats_round_zscore + turn_taking_index_zscore + (1|game_id)",data=df)

varnames = c("n_chats_round_zscore", "turn_taking_index_zscore")
names(varnames) = c("# Chats in round", "Turn-taking index")

ht_collab_score = export_summs(model_collab_score_verylow,
                               model_collab_score_low,
                               model_collab_score_moderate,
                               model_collab_score_high,
                               model_collab_score_veryhigh,
                               model_collab_score_overall,
                               model.names = c("Very Low","Low","Moderate","High","Very High","Overall"),
                               coefs=varnames,
                               error_format = "\n({std.error})\n[{conf.low}, {conf.high}]\nP = {p.value}",
                               error_pos = "same",
                               digits=3,
                               statistics=c(N="nobs"),
                               ci_level=1-0.05/n_comparisons,
                               stars = c(`***` = 0.001/n_comparisons, 
                                         `**` = 0.01/n_comparisons, 
                                         `*` = 0.05/n_comparisons))

ht_collab_duration = export_summs(model_collab_duration_verylow,
                               model_collab_duration_low,
                               model_collab_duration_moderate,
                               model_collab_duration_high,
                               model_collab_duration_veryhigh,
                               model_collab_duration_overall,
                               model.names = c("Very Low","Low","Moderate","High","Very High","Overall"),
                               coefs=varnames,
                               error_format = "\n({std.error})\n[{conf.low}, {conf.high}]\nP = {p.value}",
                               error_pos = "same",
                               digits=3,
                               statistics=c(N="nobs"),
                               ci_level=1-0.05/n_comparisons,
                               stars = c(`***` = 0.001/n_comparisons, 
                                         `**` = 0.01/n_comparisons, 
                                         `*` = 0.05/n_comparisons))

ht_collab_efficiency = export_summs(model_collab_efficiency_verylow,
                               model_collab_efficiency_low,
                               model_collab_efficiency_moderate,
                               model_collab_efficiency_high,
                               model_collab_efficiency_veryhigh,
                               model_collab_efficiency_overall,
                               model.names = c("Very Low","Low","Moderate","High","Very High","Overall"),
                               coefs=varnames,
                               error_format = "\n({std.error})\n[{conf.low}, {conf.high}]\nP = {p.value}",
                               error_pos = "same",
                               digits=3,
                               statistics=c(N="nobs"),
                               ci_level=1-0.05/n_comparisons,
                               stars = c(`***` = 0.001/n_comparisons, 
                                         `**` = 0.01/n_comparisons, 
                                         `*` = 0.05/n_comparisons))


wb = huxtable::as_Workbook(ht_collab_score, Workbook=wb, sheet = "Collab v. Score")
wb = huxtable::as_Workbook(ht_collab_duration, Workbook=wb, sheet = "Collab v. Duration")
wb = huxtable::as_Workbook(ht_collab_efficiency, Workbook=wb, sheet = "Collab v. Efficiency")

# openxlsx::saveWorkbook(wb, "csop2_stats.xlsx", overwrite=T)




```


```{r}
model_chats = lmer("n_chats_round_zscore ~ p1_score_mean_zscore + rme_mean_zscore + (1|game_id)",data=df)
model_turntaking = lmer("turn_taking_index_zscore ~ p1_score_mean_zscore + rme_mean_zscore + (1|game_id)",data=df)

varnames = c("p1_score_mean_zscore", "rme_mean_zscore")
names(varnames) = c("Skill", "Social perceptiveness")

ht_behavior = export_summs(model_chats,
                               model_turntaking,
                               model.names = c("# Chats in round","Turn-taking index"),
                               coefs=varnames,
                               error_format = "\n({std.error})\n[{conf.low}, {conf.high}]\nP = {p.value}",
                               error_pos = "same",
                               digits=3,
                               statistics=c(N="nobs"),
                               ci_level=1-0.05/n_comparisons,
                               stars = c(`***` = 0.001/n_comparisons, 
                                         `**` = 0.01/n_comparisons, 
                                         `*` = 0.05/n_comparisons))

wb = huxtable::as_Workbook(ht_behavior, Workbook=wb, sheet = "Comp v. Behavior")

openxlsx::saveWorkbook(wb, "csop2_stats.xlsx", overwrite=T)

```

```{r}
# wald test 
contest(model_score_overall, L=c(0,1,-1,0,0))
contest(model_score_overall, L=c(0,1,0,-1,0))
```

